info_list = []

def menu():
    print("学生管理系统 V1.0")
    print(" 1:添加学生")
    print(" 2:删除学生")
    print(" 3:修改学生")
    print(" 4:查询学生")
    print(" 5:显示所有学生")
    print(" 6:保存数据")
    print(" 7:退出系统")
    print("---------------------------")

def addinfo():
    global info_list
    new_info = {}
    new_name = input('请输入姓名:')
    new_tel = input('请输入手机号:')
    new_qq = input('请输入QQ:')
    for tempinfo in info_list:
        if tempinfo['name'] == new_name:
            print('你输入的姓名已经存在！')
            return
    new_info['name'] = new_name
    new_info['tell'] = new_tel
    new_info['qq'] = new_qq
    info_list.append(new_info)

def deleteinfo():
    global info_list
    i = int(input('请输入你想删除的下标：'))
    if 0 <= i < len(info_list):
        name = input('请输入你要删除的学生姓名：')
        if info_list[i]['name'] == name:
            del info_list[i]
        else:
            print('未找到你要删除的学生名！')

def modifyinfo():
    global info_list
    i = int(input('请输入你想修改的下标：'))
    if 0 <= i < len(info_list):
        print('你想修改的学生信息如下：')
        print('姓名：%s,电话：%s,qq号：%s' % (info_list[i]['name'],info_list[i]['tell'],info_list[i]['qq']))
        name = input('你要把姓名修改成啥？')
        tellnum = input('你要把电话号码修改成啥？')
        qqnum = input('你要把QQ号修改成啥？')
        info_list[i]['name'] = name
        info_list[i]['tell'] = tellnum
        info_list[i]['qq'] = qqnum
    else:
        print('你想修改的下标不存在')


def searchinfo():
    name = input('请输入你想查找的学生姓名：')
    for tempinfo in info_list:
        if tempinfo['name'] == name:
            print('姓名\t\t手机号\t\tQQ')
            print('%s \t\t%s\t\t%s' % (tempinfo['name'],tempinfo['tell'],tempinfo['qq']))
        else:
            print('未找到你想查找的学生姓名')

def allinfo():
    print('姓名\t\t手机号\t\tQQ')
    for tempinfo in info_list:
        print('%s \t\t%s\t\t%s' % (tempinfo['name'], tempinfo['tell'], tempinfo['qq']))


def saveinfo():
    f = open('MYZ.txt', 'w', encoding='utf-8')
    f.write(str(info_list))
    f.close()

def loadinfo():
    global info_list
    f = open('MYZ.txt', 'r', encoding='utf-8')
    ret = f.read()
    info_list = eval(ret)
    f.close()

def main():
    loadinfo()
    while True:
        print(menu())
        i = int(input('请输入操作对应的数字：'))
        if i == 1:
            addinfo()
        elif i == 2:
            deleteinfo()
        elif i == 3:
            modifyinfo()
        elif i == 4:
            searchinfo()
        elif i == 5:
            allinfo()
        elif i == 6:
            saveinfo()
        elif i == 7:
            break
        else:
            print('你输入的数据不合法！')
        input('请输入任意数字继续:')


main()

